$(function () {
    var lang = GetQueryString('lang');
    if (lang !== null) {
        $.cookie('locale', lang, {expires: 365, path: '/'});
    } else if ($.cookie('locale') === undefined){
        lang = (navigator.language || navigator.userLanguage).substr(0, 2);
        $.cookie('locale', lang, {expires: 365, path: '/'});
    } else {
        lang = $.cookie('locale');
    }
    $('#language').find('.language').text(lang.indexOf('en') !== -1 ? 'English' : '简体中文');
    jQuery.i18n.properties({
        name: 'charts',
        path: basePath + 'js/properties/',
        mode: 'map',
        language: lang || 'zh',
        async: false,
        callback: function () {
            $('[i18n]').each(function () {
                $(this).text($.i18n.prop($(this).attr('i18n')));
            });
            $('[i18n-title]').each(function () {
                $(this).attr('title', ($.i18n.prop($(this).attr('i18n-title'))));
            });
        }
    });
    window.$headerObj = {
        main: $('header'),
        page: {
            overview: {
                url: 'index/page',
                name: $.i18n.prop('share.title.zl'),
                title: '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>',
                query: ['hospital', 'type', 'assets'],
                tag: 'hospital'
            },
            hospitalIndex: {
                url: 'charts/area/allocation/hospital-page',
                name: $.i18n.prop('share.title.fx'),
                title: '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>',
                query: ['hospital', 'type', 'assets'],
                tag: 'hospital'
            },
            typeIndex: {
                url: 'charts/area/allocation/type-page',
                name: $.i18n.prop('share.title.sblx'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/allocation/type-page">' + $.i18n.prop('share.title.sblx') + '</a>'
                ),
                query: ['type', 'hospital', 'assets'],
                tag: 'type'
            },
            modelIndex: {
                url: 'charts/area/allocation/model-page',
                name: $.i18n.prop('share.title.sbxh'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/allocation/model-page">' + $.i18n.prop('share.title.sbxh') + '</a>'
                ),
                query: ['model', 'hospital', 'assets'],
                tag: 'model'
            },
            hospitalCost: {
                url: 'charts/area/cost/hospital-page',
                name: $.i18n.prop('share.title.fx') + '-' + $.i18n.prop('share.title.yyxl'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/cost/hospital-page">' + $.i18n.prop('share.title.yyxl') + '</a>'
                ),
                query: ['hospital', 'type', 'assets'],
                tag: 'hospital'
            },
            typeCost: {
                url: 'charts/area/cost/type-page',
                name: $.i18n.prop('share.title.sblx') + '-' + $.i18n.prop('share.title.yyxl'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/allocation/type-page">' + $.i18n.prop('share.title.sblx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/cost/type-page">' + $.i18n.prop('share.title.yyxl') + '</a>'
                ),
                query: ['type', 'hospital', 'assets'],
                tag: 'type'
            },
            modelCost: {
                url: 'charts/area/cost/model-page',
                name: $.i18n.prop('share.title.sbxh') + '-' + $.i18n.prop('share.title.yyxl'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/allocation/model-page">' + $.i18n.prop('share.title.sbxh') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/cost/model-page">' + $.i18n.prop('share.title.yyxl') + '</a>'
                ),
                query: ['model', 'hospital', 'assets'],
                tag: 'model'
            },
            hospitalMaintain: {
                url: 'charts/area/maintain/hospital-page',
                name: $.i18n.prop('share.title.fx') + '-' + $.i18n.prop('share.title.wxqk'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/maintain/hospital-page">' + $.i18n.prop('share.title.wxqk') + '</a>'
                ),
                query: ['hospital', 'type', 'brand', 'maintainer', 'assets'],
                tag: 'hospital'
            },
            typeMaintain: {
                url: 'charts/area/maintain/type-page',
                name: $.i18n.prop('share.title.sblx') + '-' + $.i18n.prop('share.title.wxqk'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/allocation/type-page">' + $.i18n.prop('share.title.sblx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/maintain/type-page">' + $.i18n.prop('share.title.wxqk') + '</a>'
                ),
                query: ['type', 'hospital', 'brand', 'maintainer', 'assets'],
                tag: 'type'
            },
            modelMaintain: {
                url: 'charts/area/maintain/model-page',
                name: $.i18n.prop('share.title.sbxh') + '-' + $.i18n.prop('share.title.wxqk'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/allocation/model-page">' + $.i18n.prop('share.title.sbxh') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/maintain/model-page">' + $.i18n.prop('share.title.wxqk') + '</a>'
                ),
                query: ['model', 'hospital', 'brand', 'maintainer', 'assets'],
                tag: 'model'
            },
            hospitalMaintenance: {
                url: 'charts/area/maintenance/hospital-page',
                name: $.i18n.prop('share.title.fx') + '-' + $.i18n.prop('share.title.whqk'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/maintenance/hospital-page">' + $.i18n.prop('share.title.whqk') + '</a>'
                ),
                query: ['hospital', 'type', 'assets'],
                tag: 'hospital'
            },
            typeMaintenance: {
                url: 'charts/area/maintenance/type-page',
                name: $.i18n.prop('share.title.sblx') + '-' + $.i18n.prop('share.title.whqk'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/allocation/type-page">' + $.i18n.prop('share.title.sblx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/maintenance/type-page">' + $.i18n.prop('share.title.whqk') + '</a>'
                ),
                query: ['type', 'hospital', 'assets'],
                tag: 'type'
            },
            modelMaintenance: {
                url: 'charts/area/maintenance/model-page',
                name: $.i18n.prop('share.title.sbxh') + '-' + $.i18n.prop('share.title.whqk'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/allocation/model-page">' + $.i18n.prop('share.title.sbxh') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/maintenance/model-page">' + $.i18n.prop('share.title.whqk') + '</a>'
                ),
                query: ['model', 'hospital', 'brand', 'assets'],
                tag: 'model'
            },
            hospitalBrand: {
                url: 'charts/area/brand/hospital-page',
                name: $.i18n.prop('share.title.fx') + '-' + $.i18n.prop('share.table.head.sbcj'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/brand/hospital-page">' + $.i18n.prop('share.table.head.sbcj') + '</a>'
                ),
                query: ['hospital', 'type', 'model', 'assets'],
                tag: 'hospital'
            },
            typeBrand: {
                url: 'charts/area/brand/type-page',
                name: $.i18n.prop('share.title.sblx') + '-' + $.i18n.prop('share.table.head.sbcj'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/allocation/type-page">' + $.i18n.prop('share.title.sblx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/brand/type-page">' + $.i18n.prop('share.table.head.sbcj') + '</a>'
                ),
                query: ['type', 'hospital', 'model', 'assets'],
                tag: 'type'
            },
            hospitalMaintainer: {
                url: 'charts/area/maintainer/hospital-page',
                name: $.i18n.prop('share.title.fx') + '-' + $.i18n.prop('share.table.head.wxs'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/maintainer/hospital-page">' + $.i18n.prop('share.table.head.wxs') + '</a>'
                ),
                query: ['hospital', 'type', 'model'],
                tag: 'hospital'
            },
            typeMaintainer: {
                url: 'charts/area/maintainer/type-page',
                name: $.i18n.prop('share.title.sblx') + '-' + $.i18n.prop('share.table.head.wxs'),
                title: (
                    '<a href="' + basePath + 'charts/area/allocation/hospital-page">' + $.i18n.prop('share.title.fx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/allocation/type-page">' + $.i18n.prop('share.title.sblx') + '</a>-' +
                    '<a href="' + basePath + 'charts/area/maintainer/type-page">' + $.i18n.prop('share.table.head.wxs') + '</a>'
                ),
                query: ['type', 'hospital', 'model'],
                tag: 'type'
            }
        },
        init: function () {
            this.setPage();
            this.setTitle();
            this.queryTime();
            this.bindEvent();
        },
        setPage: function () {
            for (var key in this.page) {
                if (location.href.indexOf(this.page[key].url) !== -1) {
                    this.page.current = key;
                    this.page.previous = localStorage.getItem('previous_page');
                    localStorage.setItem('previous_page', key);
                    this.page.tag = this.page[key].tag;
                    break;
                }
            }
            if (this.page.current.indexOf('Index') !== -1) {
                this.main.find('.select_month').closest('tr').hide();
            }
        },
        setTitle: function () {
            var headerPage = this.main.find('#header-page'),
                str = this.page[this.page.current].title;
            headerPage.html(str || headerPage.html());
        },
        queryTime: function () {
            var $this = this;
            publicAjax(basePath + 'area/search/time', 'post', null, function (data) {
                $this.main.find('.time').html($.i18n.prop('share.title.jzrq') + '：' + data.data.time);
            }, true);
        },
        bindEvent: function () {
            /* 登出 */
            this.main.find('.header-logout').on('click', this.logout.bind(this));
        },
        reset: function () {
            localStorage.setItem('search_params', null);
            location.reload();
        },
        logout: function () {
            this.searchObj.reset();
            location.href = basePath + '/logout';
        }
    };


    // loading();
    setTimeout($headerObj.init.bind($headerObj), 10);
});